/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DAO.util.ConectaBanco;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.Login;
import model.Privilegio;

/**
 *
 * @author Alex Onodera
 */
public class PrivilegioDAO {

    public boolean criar(Login login) {
        try {
            Connection conexao = ConectaBanco.getConexao();

            String sql = "insert into privilegio(id_login,administrador, atendente, instrutor, aluno) values (?,false,false,false,false)";

            PreparedStatement pstmt = conexao.prepareCall(sql);

            System.out.println(sql);

            pstmt.setInt(1, login.getId());



            pstmt.execute();

            conexao.close();
            return true;

        } catch (SQLException sql) {
            sql.printStackTrace();
            return false;

        }

    }

    public boolean definirPrivilegio(Privilegio privilegio) {
        try {
            Connection conexao = ConectaBanco.getConexao();

            String sql = "update privilegio set administrador=?, atendente=?, instrutor=?, aluno=? where id_login=?";

            PreparedStatement pstmt = conexao.prepareCall(sql);

            pstmt.setBoolean(1, privilegio.isAdministrador());
            pstmt.setBoolean(2, privilegio.isAtendente());
            pstmt.setBoolean(3, privilegio.isInstrutor());
            pstmt.setBoolean(4, privilegio.isAluno());
            pstmt.setInt(5, privilegio.getLogin().getId());
            
            System.out.println(privilegio.getLogin().getId());

        
            pstmt.execute();

            conexao.close();
            return true;
        } catch (SQLException sql) {
            System.out.println("Erro ao definir privilégios");
            return false;

        }
    }

    public Privilegio buscarPrivilegioByCodigo(int id) throws ClassNotFoundException, SQLException {
        Connection conexao = ConectaBanco.getConexao();
        PreparedStatement sql = conexao.prepareStatement("select * from privilegio where id_login=?");
        sql.setInt(1, id);
        ResultSet rs = sql.executeQuery();
        Privilegio privilegio = new Privilegio();
        while (rs.next()) {
            privilegio.setId(rs.getInt("Id"));
            privilegio.setAdministrador(rs.getBoolean("administrador"));
            privilegio.setAluno(rs.getBoolean("aluno"));
            privilegio.setAtendente(rs.getBoolean("atendente"));
            privilegio.setInstrutor(rs.getBoolean("instrutor"));
            Login login = new Login();
            login.setId(rs.getInt("id_login"));
            privilegio.setLogin(login);

        }
        conexao.close();
        return privilegio;
    }
}
